package com.chnyu
import Array._
object test1 {
  def add(x:Int):Int={//冒号后面的Int是返回值
    var sum=0;
    sum+=x;
    return sum;

  }

  val f1=(x:Int,y:Int)=>x+y;

  def m1(f:(Int,Int)=>Int):Int={
    f(5,8)
  }

  def main(args: Array[String]): Unit = {
//    println("zxd")
    var sum1=add(6);
    println(sum1)
//调用匿名函数
    var s1=f1(5,6)
    println(s1)
//通过方法实现函数
    var s2=m1(f1)
    println(s2)

    //方法转函数
//    1、下划线
    val f2 =m1 _
    val s3=f2(f1)
    println(s3)
    //2、x=>m(x)匿名函数（x:Int）=>m(x) var a=funtion()
    var f3=(x:Int)=>x+3
    var s4=f3(6)
    println(s4)

//    3、高阶函数：能够接受函数作为参数的一个方法
    //在函数func中输入参数为（整型，字符串），输出为（字符串，整型）
  val func:(Int,String)=>(String,Int)={
      (x,y)=>(y,x)
    }
  //定义一个方法
    def method(people:(Int,String)=>(String,Int)):Int={
      val result=people(20,"小明")
      println(result._1,result._2)
  return result._2
    }
    println(method(func))


    var x="hello"
    val y=" world"
    println(x.length)
    println(x+y)

    val buf=new StringBuilder
    buf.append("asd").append("qwe").append('a');
    println(buf.toString())

//可以同时给多个变量初始化
    var a=10;
    var b,c=100
  //元组
    val tuple1=(1,"aa")
    println(tuple1+" "+tuple1._1+" "+tuple1._2)
    //数组
    var array =Array("a","x","w")

    println("==========")
    for(x<-array){
      println(x)
    }
    println("==========")

    var data_ar=Array(1,2,3,4,5)
    var summ=0
    for(i<-0 to data_ar.length-1){
      summ+=data_ar(i)
    }
    println(summ)

    var max=data_ar(0)
    for(i<-0 to data_ar.length-1){
      if(data_ar(i)>max){
        max=data_ar(i)
      }
    }
    println(max)


    var a1=Array(1.9,2.3,3.6,5.4)
    var a2=Array(1.0,2.5,6.9,4.7)
    var array3=concat(a1,a2);
    for(i<-0 to array3.length-1){
      println(array3(i))
    }

    //整型列表
    val list=List(1,2,3,4)
    println(list)

    val list2=List("aa","bb","cc","dd")
    println(list2)

    val e:List[Nothing]=List()
    println(e)
    val list3=List()
    println(list3)
    println(list2.head)

    println(list2.tail)

    //判断空列表
    println(list2.isEmpty)
    if("true".equals(list2.isEmpty)){
      println("空")
    }else{
      println("bu空")
    }

    //列表的连接方式
    var con1=List.concat(list,list2)
    println(con1)

    //运算符:::
    var con2=list:::list2
    println(con2)

    //运算符.:::  是list2在前
    var con3=list.:::(list2)
    println(con3)

    var con4=list2:::list
    println(con4)
    //反着说
    println(con4.reverse)

    //迭代器
    val it=Iterator("baidu","asdf","qwe","vds")
    println("=================")
    while(it.hasNext){
    println(it.next())
    }
    println("=================")
    val it3=Iterator("baidu","asdf","qwe","vds")
    //整型迭代器
    val it1=Iterator(15,53,52,214)
    val it2=Iterator(54,5,2,1)
    println(it1.max)
    println(it2.min)
    println(it3.length)
    //println(it1.min)


    val map1=Map(1->"dfg",2->"we")
    println(map1.keys)
    println(map1.values)
    val key=map1.keySet

    val aa1=Array(1,2,3)
    val aa2=Array(4,5,6)
    val aa3=Array(7,8,9)
    var su1=0
    var su2=0
    var su3=0
    for(i<-0 to aa1.length-1){
      su1+=aa1(i)
    }
    println(su1)
    println(su1/(aa1.length-1))
    println("==========")
    for(i<-0 to aa2.length-1){
      su2+=aa2(i)
    }
    println(su2)
    println(su2/(aa2.length-1))
    println("==========")
    for(i<-0 to aa3.length-1){
      su3+=aa3(i)
    }
    println(su3)
    println(su3/(aa3.length-1))
    println("==========")
  for(s<-map1)println(s)
    println("==========")
    map1.foreach(println)
  }

}
